package calorie.com.calorie.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;

import com.lxh.baselibray.base.XFragment;
import com.lxh.baselibray.dialog.AlertDialog;
import com.lxh.baselibray.mvp.MvpFragment;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.lxh.baselibray.view.TitleView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import calorie.com.calorie.R;
import calorie.com.calorie.activity.CoinChargingActivity;
import calorie.com.calorie.activity.GradeActivity;
import calorie.com.calorie.activity.MyInvitationActivity;
import calorie.com.calorie.activity.PersonalInfomationActivity;
import calorie.com.calorie.activity.RecordActivity;
import calorie.com.calorie.activity.TransferActivity;
import calorie.com.calorie.activity.WithdrawMoneyActivity;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.GetAssetsContact;
import calorie.com.calorie.contact.UserInfoContact;
import calorie.com.calorie.response.AssertInfomationResponce;
import calorie.com.calorie.response.UserAssets;
import calorie.com.calorie.response.UserInfoResponce;
import calorie.com.calorie.util.BLEUpdateBroadcastReceiver;
import calorie.com.calorie.util.NumberFormatUtils;
import calorie.com.calorie.util.StaticValue;
import retrofit2.Response;

/**
 * 我的钱包
 */
public class MyWalletFragment extends MvpFragment<GetAssetsContact.GetAssetsPresent> implements GetAssetsContact.IgetAssets ,UserInfoContact.IUserInfoView{
    //当前等级
    @BindView(R.id.lay_WalletGrade)
    LinearLayout lay_WalletGrade;
    //充币
    @BindView(R.id.lay_WalletCoinCharging)
    LinearLayout lay_WalletCoinCharging;
    //提币
    @BindView(R.id.lay_WalletWithdrawMoney)
    LinearLayout lay_WalletWithdrawMoney;
    //转帐
    @BindView(R.id.lay_WalletTransfer)
    LinearLayout lay_WalletTransfer;
    //我邀请的
    @BindView(R.id.lay_WalletMyInvitation)
    LinearLayout lay_WalletMyInvitation;
    @BindView(R.id.titleView)
    TitleView titleView;
    @BindView(R.id.tv_WalletCALBalance)
    TextView tvWalletCALBalance;
    @BindView(R.id.tv_WalletVITBalance)
    TextView tvWalletVITBalance;
    @BindView(R.id.tv_level)
    TextView tvLevel;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    UserInfoContact.UserInfoPresent userInfoPresent;
    @Override
    public void initBeforView(Bundle savedInstanceState) {
        super.initBeforView(savedInstanceState);
        userInfoPresent=new UserInfoContact.UserInfoPresent();
        userInfoPresent.attach(this);
}

    @Override
    public void initData(Bundle savedInstanceState) {
        titleView.setRightOnclickListener(v -> startActivity(new Intent(context, RecordActivity.class)));

        BLEUpdateBroadcastReceiver.cal_num_view.add(tvWalletCALBalance);
        BLEUpdateBroadcastReceiver.vit_num_view.add(tvWalletVITBalance);
        mPresent.getAssets(context);
        userInfoPresent.getUserInfo(context);
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresent.getAssets(context);
                userInfoPresent.getUserInfo(context);
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_my_wallet;
    }

    @OnClick({R.id.lay_WalletGrade, R.id.lay_WalletCoinCharging, R.id.lay_WalletWithdrawMoney, R.id.lay_WalletTransfer, R.id.lay_WalletMyInvitation})
    public void onViewClicked(View view) {
        String purAddress = SPUtils.getString(Constant.PURSE_ADDRESS, null, context);
        switch (view.getId()) {
            case R.id.lay_WalletGrade:
                if (StaticValue.user != null && StaticValue.user.getLevel() != 0) {
                    startActivity(new Intent(context, GradeActivity.class));
                } else {
                    ToastUtils.showLongToast(context, "帐号未激活");
                }
                break;
            case R.id.lay_WalletCoinCharging:
                if (ObjectUtils.isEmpty(purAddress)){
                    showRemindDialog("您还未绑定钱包地址,去绑定?");
                    return;
                }
                startActivity(new Intent(context, CoinChargingActivity.class));
                break;
            case R.id.lay_WalletWithdrawMoney:
                if (ObjectUtils.isEmpty(purAddress)){
                    showRemindDialog("您还未绑定钱包地址,去绑定?");
                    return;
                }
                startActivity(new Intent(context, WithdrawMoneyActivity.class));
                break;
            case R.id.lay_WalletTransfer:
                startActivity(new Intent(context, TransferActivity.class));
                break;
            case R.id.lay_WalletMyInvitation:
                startActivity(new Intent(context, MyInvitationActivity.class));
                break;
        }
    }

    @Override
    public void getAssetsResult(Response<AssertInfomationResponce> response) {
        refresh.finishRefresh();
        if (response.body().getCode()== Constant.SUCCESS_CODE){
            Gson gs = new Gson();
            StaticValue.userAssets = gs.fromJson(gs.toJson(response.body().getData()), UserAssets.class);//保存用户资产信息

            AssertInfomationResponce.DataBean data = response.body().getData();
            if (ObjectUtils.isEmpty(data))return;
            SPUtils.putString(Constant.PURSE_ADDRESS,data.getMoneyAddress(),context);
            tvWalletCALBalance.setText(NumberFormatUtils.keepTwoDecimal(data.getCalNum()));
            tvWalletVITBalance.setText(NumberFormatUtils.keepTwoDecimal(data.getTivNum()));
        }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }



    @Override
    protected GetAssetsContact.GetAssetsPresent createPresent() {
        return new GetAssetsContact.GetAssetsPresent();
    }

    @Override
    public void getUserInfoData(Response<UserInfoResponce> response) {
        refresh.finishRefresh();
        if (response.body().getCode()== Constant.SUCCESS_CODE){
            UserInfoResponce.DataBean data = response.body().getData();
            if (ObjectUtils.isEmpty(data))return;
            String levelText = "";
            switch (data.getLevel()) {//级别0未激活1菜鸟 2达人3健将 4精英 5教练6大师7王者
                case 0:
                    levelText = "帐号未激活";
                    break;
                case 1:
                    levelText = "菜鸟";
                    break;
                case 2:
                    levelText = "达人";
                    break;
                case 3:
                    levelText = "健将";
                    break;
                case 4:
                    levelText = "精英";
                    break;
                case 5:
                    levelText = "教练";
                    break;
                case 6:
                    levelText = "大师";
                    break;
                case 7:
                    levelText = "王者";
                    break;
            }
            tvLevel.setText(levelText);
        }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }

    AlertDialog remindDialog = null;
    private void showRemindDialog(String content) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_reimind)
                .setText(R.id.tv_content, content)
                .setWidthAndHeight(SizeUtils.dp2px(context, 279), SizeUtils.dp2px(context, 157))
                .setOnClickListener(R.id.tv_sure, v -> {
                   startActivity(new Intent(context, PersonalInfomationActivity.class));
                    remindDialog.dismiss();
                }).setOnClickListener(R.id.tv_cancle, v -> remindDialog.dismiss());
        remindDialog = builder.create();
        remindDialog.show();
    }

    @Override
    public void onResume() {
        super.onResume();
         userInfoPresent.getUserInfo(context);
    }
}
